/********************************************************************
	created:	2011/12/21
	filename: 	main.cpp
	author:		Mwolf
	
	purpose:	
*********************************************************************/

#include "GWTimer.h"
#include "Renderer/GWRenderWindow.h"
#include "Renderer/GWRenderDevice.h"
#include "GWInputControl.h"

//#define VIEWPORT_TEST
void ViewportTest();
void DrawAnimatedAxises();

int rw_half_width, rw_half_height;
float aspect;

Vector3 Transform( const Vector3& vec, const Matrix4& mat );

#if defined GW_PLATFORM_WIN32
#include <windows.h>
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
#else
int main()
#endif
{
	GW_RENDER_WINDOW* render_window;

#if defined GW_PLATFORM_PSP
	render_window = GWWnd_CreateRenderWindow();
#else
	render_window = GWWnd_CreateRenderWindow( 800, 600, "Render Test" );
#endif

	GWWnd_GetSize( render_window, &rw_half_width, &rw_half_height );
	aspect = (float)rw_half_width / (float)rw_half_height;
	rw_half_width /= 2;
	rw_half_height /= 2;

	GWInput_InitializeDevice( render_window );

	RenderSystem::Initialize( render_window );

	while ( HandleWindowMessage() )
	{
		RenderSystem::BeginRender();
		RenderSystem::Clear( GWColor( .5f, .5f, 1.f) );

		RenderSystem::SetPerspectiveProjMatrix( 45.f, aspect, 0.1f, 100.f );

		RenderSystem::SetViewMatrix( Vector3(10.f, 10.f, 10.f), Vector3::ZERO );

		//RenderSystem::LoadIdentityModelMatrix();

#if !defined (VIEWPORT_TEST)
		/*
		Vector3 v1 = Vector3(-1.f, -1.f, 0.f),
				v2 = Vector3(1.f, 1.f, 0.f),
				v3 = Vector3(2.f, 2.f, 0.f);

		static float angle = 0.f;
		//angle += DEGREE(1.f);
		angle = DEGREE(.05f) * GWSys_GetTickCount();
		Matrix4 rotMat = Matrix4::BuildTranslationMatrix( Vector3( 0.f, 1.f, 0.f ) ) * Matrix4::BuildRotationMatrix( angle, ROTATION_AXIS_X );
		RenderSystem::SetModelMatrix( rotMat );
		RenderSystem::DrawColoredSprite( Vector2(v2.x, v2.y), Vector2(v3.x, v3.y), GWColor(1.f, .5f, .5f) );
		RenderSystem::DrawWireframeRect( Vector2(v1.x, v1.y), Vector2(v2.x, v2.y), GWColor::YELLOW );

		Vector3 v1_proj = Transform(v1, rotMat),
				v2_proj = Transform(v2, rotMat),
				v3_proj = Transform(v3, rotMat);

		// Project tile to the collision plane
		RenderSystem::SetModelMatrix( Matrix4::IDENTITY );
		RenderSystem::DrawColoredSprite( Vector2(v2_proj.x, v2_proj.y), Vector2(v3_proj.x, v3_proj.y), GWColor(1.f, .5f, .5f), -5.f );
		RenderSystem::DrawWireframeRect( Vector2(v1_proj.x, v1_proj.y), Vector2(v2_proj.x, v2_proj.y), GWColor::YELLOW, -5.f );
		*/

		DrawAnimatedAxises();
#else
		ViewportTest();
#endif

		RenderSystem::EndRender();

		RenderSystem::Flush();
	}

	GWInput_DestroyDevice();
	RenderSystem::Destroy();

	GWWnd_DestroyRenderWindow( render_window );

	return 0;
}

void ViewportTest()
{
	RenderSystem::SetViewport( 0, 0, rw_half_width, rw_half_height );
	RenderSystem::Clear( GWColor(.5f, 1.f, .5f) );

	RenderSystem::SetViewport( rw_half_width, 0, rw_half_width, rw_half_height );
	RenderSystem::Clear( GWColor(.5f, .5f, 1.f) );

	RenderSystem::SetViewport( 0, rw_half_height, rw_half_width, rw_half_height );
	RenderSystem::Clear( GWColor(1.f, 1.f, .5f) );

	RenderSystem::SetViewport( rw_half_width, rw_half_height, rw_half_width, rw_half_height );
	RenderSystem::Clear( GWColor(1.f, .5f, .5f) );
}

Vector3 Transform( const Vector3& vec, const Matrix4& mat )
{
	float x, y, z, w;

	x = vec.x * mat.m11 + vec.y * mat.m21 + vec.z * mat.m31 + 1.f * mat.m41;
	y = vec.x * mat.m12 + vec.y * mat.m22 + vec.z * mat.m32 + 1.f * mat.m42;
	z = vec.x * mat.m13 + vec.y * mat.m23 + vec.z * mat.m33 + 1.f * mat.m43;
	w = vec.x * mat.m14 + vec.y * mat.m24 + vec.z * mat.m34 + 1.f * mat.m44;

	return Vector3(x, y, z);
}

void DrawAnimatedAxises()
{
	static float angle = 0.f;

	angle = DEGREE(.05f) * GWSys_GetTickCount();
	Matrix4 rotMat = Matrix4::BuildRotationMatrix( angle, ROTATION_AXIS_X );
	RenderSystem::SetModelMatrix( rotMat );
	RenderSystem::DrawLine(Vector3::ZERO, Vector3(2.f, 0.f, 0.f), GWColor::RED);
	RenderSystem::DrawLine(Vector3::ZERO, Vector3(0.f, 2.f, 0.f), GWColor::GREEN);
	RenderSystem::DrawLine(Vector3::ZERO, Vector3(0.f, 0.f, 2.f), GWColor::BLUE);

	rotMat = Matrix4::BuildRotationMatrix( angle, ROTATION_AXIS_Y ) * Matrix4::BuildTranslationMatrix( Vector3( 0.f, 0.f, 5.f ) );
	RenderSystem::SetModelMatrix( rotMat );
	RenderSystem::DrawLine(Vector3::ZERO, Vector3(2.f, 0.f, 0.f), GWColor::RED);
	RenderSystem::DrawLine(Vector3::ZERO, Vector3(0.f, 2.f, 0.f), GWColor::GREEN);
	RenderSystem::DrawLine(Vector3::ZERO, Vector3(0.f, 0.f, 2.f), GWColor::BLUE);

	rotMat = Matrix4::BuildRotationMatrix( angle, ROTATION_AXIS_Z ) * Matrix4::BuildTranslationMatrix( Vector3( 5.f, 0.f, 0.f ) );
	RenderSystem::SetModelMatrix( rotMat );
	RenderSystem::DrawLine(Vector3::ZERO, Vector3(2.f, 0.f, 0.f), GWColor::RED);
	RenderSystem::DrawLine(Vector3::ZERO, Vector3(0.f, 2.f, 0.f), GWColor::GREEN);
	RenderSystem::DrawLine(Vector3::ZERO, Vector3(0.f, 0.f, 2.f), GWColor::BLUE);
}
